package com.deimos.dataBase.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.deimos.dataBase.Scenario;

/**
 * Servlet implementation class BuildDBServlet
 */
public class SetScenarioServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private SimpleDateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SetScenarioServlet() {
	super();
	// TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// TODO Auto-generated method stub
	PrintWriter out = null;
	out = response.getWriter();
	out.print(this.setScenario(request.getQueryString(), request.getRequestURI()));

    }

    public String setScenario(String queryString, String uri) {
	String response = "";
	StringBuffer scenarioId = new StringBuffer();
	Date dateTime = new Date();

	if (parseRequest(queryString, scenarioId, dateTime) && Scenario.setScenario(scenarioId.toString(), dateTime)) {
	    if (scenarioId.toString().toLowerCase().compareTo("null") == 0)
		response = "Scenario removed.";
	    else {
		response = "Scenario set to: \n";
		response = response + "ScenarioId: " + scenarioId + "\n";
		if (Scenario.now == true)
		    response = response + "StartTime: Now\n";
		else 
		    response = response + "StartTime: " + DATEFORMAT.format(dateTime) + "\n";
	    }
	} else {
	    response = "Incorrect Scenario. The request must be: ";
	    response = response + uri + "?Scenario=scenarioId&StartTime=yyyy-MM-ddTHH:mm:ss";
	}
	return response;
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// TODO Auto-generated method stub
    }

    private boolean parseRequest(String request, StringBuffer scenarioId, Date dateTime) {
	
	try {
	    if (request.toLowerCase().contains("scenario") && request.toLowerCase().contains("starttime")) {
		String[] parameters = request.split("&");
		if (parameters.length < 2)
		    return false;
		for (String parameter : parameters) {
		    String[] atribute = parameter.split("=");
		    if (atribute.length != 2)
			return false;
		    if (atribute[0].toLowerCase().compareTo("scenario") == 0)
			scenarioId.append(atribute[1]);
		    if (atribute[0].toLowerCase().compareTo("starttime") == 0)
			if (atribute[1].toLowerCase().compareTo("now") == 0)
			    Scenario.now = true;
			else {
			    dateTime.setTime(DATEFORMAT.parse(atribute[1]).getTime());
			    Scenario.now = false;
			}
		}
	    } else if (request.toLowerCase().contains("scenario")) {
		String[] parameters = request.split("&");
		if (parameters.length < 1)
		    return false;
		for (String parameter : parameters) {
		    String[] atribute = parameter.split("=");
		    if (atribute.length != 2)
			return false;
		    if (atribute[0].toLowerCase().compareTo("scenario") == 0) {
			scenarioId.append(atribute[1]);
			if (scenarioId.toString().toLowerCase().compareTo("null") != 0)
			    return false;
		    }
		}

	    } else
		return false;
	} catch (Exception e) {
	    return false;
	}

	return true;
    }

}
